﻿using System;
using System.Collections.Generic;
using System.Text;
using POSPlus.EZBarTender.DatabaseOperations;
using System.Data.SqlClient;

namespace POSPlus.DatabaseOperations
{
	public class PrintManagerDisplay : DatabaseObject
	{
		public String Upc { get; set; }
		public String BatchNo { get; set; }
		public String JobName { get; set; }
		public int LocAisle { get; set; }
		public int LocBlock { get; set; }
		public int LocSeq { get; set; }
		public String Type { get; set; }
		public int Copies { get { return 1; } }

		public PrintManagerDisplay(String connectionString)
		{
			ConnectionString = connectionString;
			PopulateDefault();
		}
		public PrintManagerDisplay(String connectionString, String jobName, String upc)
		{
			ConnectionString = connectionString;
			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT Upc, BatchNo, JobName, LocAisle, LocBlock, LocSeq, Type " +
					"FROM POSplus_PrintManagerDisplays WHERE JobName = @JobName AND Upc = @Upc"))
				{
					command.Parameters.AddWithValue("@JobName", jobName);
					command.Parameters.AddWithValue("@Upc", upc);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						if (reader.Read())
						{
							JobName = FromDB<String>(reader["JobName"]);
							Upc = FromDB<String>(reader["Upc"]);
							BatchNo = FromDB<String>(reader["BatchNo"]);
							LocAisle = FromDB<int>(reader["LocAisle"]);
							LocBlock = FromDB<int>(reader["LocBlock"]);
							LocSeq = FromDB<int>(reader["LocSeq"]);
							Type = FromDB<String>(reader["Type"]);
						}
						else
						{
							PopulateDefault();
						}
					}
				}
				connection.Close();
			}
		}

		public PrintManagerDisplay(String jobName, String upc, String batchNo, int locAisle, int locBlock, int locSeq, String type)
		{
			JobName = jobName;
			Upc = upc;
			BatchNo = batchNo;
			LocAisle = locAisle;
			LocBlock = locBlock;
			LocSeq = locSeq;
			Type = type;
		}

		public static List<PrintManagerDisplay> GetAll(String connectionString)
		{
			List<PrintManagerDisplay> result = new List<PrintManagerDisplay>();
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT Upc, BatchNo, JobName, LocAisle, LocBlock, LocSeq, Type " +
					"FROM POSplus_PrintManagerDisplays"))
				{
					using (SqlDataReader reader = command.ExecuteReader())
					{
						while (reader.Read())
						{
							result.Add(new PrintManagerDisplay(
								FromDB<String>(reader["JobName"]),
								FromDB<String>(reader["Upc"]),
								FromDB<String>(reader["BatchNo"]),
								FromDB<int>(reader["LocAisle"]),
								FromDB<int>(reader["LocBlock"]),
								FromDB<int>(reader["LocSeq"]),
								FromDB<String>(reader["Type"])));
						}
					}
				}
				connection.Close();
			}
			return result;
		}

		protected override void PopulateDefault()
		{
			Upc = "";
			BatchNo = null;
			JobName = "";
			LocAisle = default(int);
			LocBlock = default(int);
			LocSeq = default(int);
			Type = "";
		}

		public override void Create()
		{
			if (string.IsNullOrEmpty(JobName))
				throw new ArgumentNullException("JobName", "JobName cannot be empty.");

			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("INSERT INTO POSplus_PrintManagerDisplays (" +
					"Upc, BatchNo, JobName, LocAisle, LocBlock, LocSeq, Type) VALUES (@Upc, @BatchNo, @JobName, " +
					"@LocAisle, @LocBlock, @LocSeq, @Type)", connection))
				{
					command.Parameters.AddWithValue("@Upc", Upc);
					command.Parameters.AddWithValue("@BatchNo",ToDB<String>(BatchNo));
					command.Parameters.AddWithValue("@JobName", JobName);
					command.Parameters.AddWithValue("@LocAisle", LocAisle);
					command.Parameters.AddWithValue("@LocBlock", LocBlock);
					command.Parameters.AddWithValue("@LocSeq", LocSeq);
					command.Parameters.AddWithValue("@Type", Type);
					try
					{
						command.ExecuteNonQuery();
					}
					catch
					{
						throw;
					}
				}
				connection.Close();
			}
		}

		public override void Update()
		{
			if (string.IsNullOrEmpty(JobName))
				throw new ArgumentNullException("JobName", "JobName cannot be empty.");

			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("UPDATE POSplus_PrintManagerDisplays SET " +
					"Upc = @Upc, BatchNo = @BatchNo, JobName = @JobName, LocAisle = @LocAisle, LocBlock = @LocBlock, " +
					"LocSeq = @LocSeq, Type = @Type WHERE Upc = @Upc AND JobName = @JobName"))
				{
					command.Parameters.AddWithValue("@Upc", Upc);
					command.Parameters.AddWithValue("@BatchNo", BatchNo);
					command.Parameters.AddWithValue("@JobName", JobName);
					command.Parameters.AddWithValue("@LocAisle", LocAisle);
					command.Parameters.AddWithValue("@LocBlock", LocBlock);
					command.Parameters.AddWithValue("@LocSeq", LocSeq);
					command.Parameters.AddWithValue("@Type", Type);
					command.ExecuteNonQuery();
				}
			}
		}

		public override void Delete()
		{
			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("DELETE FROM POSplus_PrintManagerDisplays WHERE JobName = " +
					"@JobName AND Upc = @Upc"))
				{
					command.Parameters.AddWithValue("@JobName", JobName);
					command.Parameters.AddWithValue("@Upc", Upc);
					command.ExecuteNonQuery();
				}
				connection.Close();
			}
		}

		public override string ToString()
		{
			return "Upc: " + this.Upc + "\nBatchNo: " + this.BatchNo + "\nJobName: " + this.JobName;
		}
	}
}
